﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Web;
using WLFramework.Data;
using WLFramework.Data.Linq;
using WLFramework.Json;

namespace SampleSite.admin.ajax.grid
{
    /// <summary>
    /// getAllPositions 的摘要说明
    /// </summary>
    public class getAllPositions : AjaxHttpHandler
    {

        public override void OnProcessRequest(HttpContext context)
        {
            int id = context.GetValueByName<int>("id");
            if (id <= 0) throw new ArgumentException("参数格式不正确!");

            using (CommandHelper db = new CommandHelper(CommandMethod.Read))
            {
                var positions = from p in db.Context.sys_Positions
                                where p.P_State == 1
                                select p;

                var rp = from p in db.Context.sys_PostRoles
                         where p.R_State == 1 && p.R_ID == id
                         select p;

                var query = from p in positions
                            join r in rp on p.P_ID equals r.P_ID
                                into temp
                            from t in temp.DefaultIfEmpty()
                            select new
                            {
                                id = p.P_ID,
                                cname = p.P_CName,
                                ename = p.P_EName,
                                org = p.sysPositionsysOrg.O_CName,
                                state = t == null ? false : true
                            };

                context.Response.Write(JSON.Instance.ToJSON(query.ToList()));
            }
        }
    }
}